const app = getApp()

import {
  getUser,
  editUser
} from '../../../api/user'
import {
  initQiniu,
  uploadSingleImg
} from '../../../api/upload'

import {
  formatTime
} from '../../../utils/util'
Page({
  data: {
    avatar: '',
    phone: '',
    formatter: '',
    birth: '',
    realName: '',
    popupShow: false,
    type: '',
    prop: '',
    tmpVal: '',
    address: '',
    columns: [],
    minDate: new Date('1900/01/01').getTime(),
    maxDate: new Date().getTime(),
    defaultIndex: 0,
    currentDate: new Date('1996/01/01').getTime()
  },
  onShow() {
    // this.setData({
    //   birth: formatTime( new Date()).substr(0,10).replace(/\//g, '-')
    // })
    this.getUserInfo()

  },
  onClickLeft() {
    // wx.showToast({
    //   title: '点击返回',
    //   icon: 'none'
    // });
    wx.navigateBack({
      delta: 1,
    })
  },
  // save(){
  //   app.Toast({
  //     message: "保存"
  //   })
  // },
  getUserInfo() {

    getUser().then(res => {
        console.log(res)
        const {
          user,
          defaultReceicer
        } = res.data
        const {
          avatar,
          phone,
          realName,
          wxName,
          gender,
          birth,

        } = user
        const {
          postProvince,
          postDistrict,
          postAddress,
          postCity,
        } = defaultReceicer
        let currentDate = new Date('1996/01/01').getTime()
        if (birth) {
          currentDate = new Date(birth.replace(/-/g, '/')).getTime()
        }

        let address = `${postProvince||''},${postCity},${postDistrict} ${postAddress}`
        this.setData({
          avatar: avatar || '',
          phone: phone || '',
          realName: realName || wxName || '',
          gender: gender || '',
          birth: birth || '',
          currentDate,
          address,
        })
      })
      .catch(err => {
        console.log(err)
      })
  },
  editUserInfo() {

    const {
      avatar,
      phone,
      realName,
      gender,
      birth
    } = this.data
    if (!realName) {
      wx.showToast({
        title: '请输入姓名',
        icon: none
      })
      return
    }

    if (!phone) {
      wx.showToast({
        title: '请输入电话',
        icon: 'none'
      })
      return
    }
    if (!gender) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return
    }
    if (!birth) {
      wx.showToast({
        title: '请选择生日',
        icon: 'none'
      })
      return
    }
    if (!avatar) {
      wx.showToast({
        title: '请选择头像',
        icon: 'none'
      })
      return
    }
    const params = {
      name: realName,
      phone: String(phone),
      gender: String(gender),
      birth,
      avatar,
    }

    editUser(params).then(res => {
        console.log(res)
        wx.showToast({
          title: '保存成功',
          icon: 'none'
        })
      })
      .catch(err => {
        console.log(err)
      })
  },
  showPopup(e) {
    // console.log(e)
    const {
      type,
      prop
    } = e.currentTarget.dataset
    let defaultIndex = 0
    let tmpVal = this.data[prop] || ''
    console.log(tmpVal, prop, type, this.data)
    let columns = []
    if (type === 'picker') {
      if (prop === 'gender') {
        columns = ['男', '女']
        tmpVal = this.data[prop] || 1
        if (this.data[prop] == 1) {
          defaultIndex = 0
        }
        if (this.data[prop] == 2) {
          defaultIndex = 1
        }

        // tmpVal = 1
      }

    }
    this.setData({
      popupShow: true,
      type,
      prop,
      tmpVal,
      columns,
      defaultIndex
    })
  },
  onPopupClose() {
    this.setData({
      popupShow: false,
      prop: '',
      type: '',
      columns: []
    })
  },
  textChange(e) {
    console.log(e)

    this.setData({
      tmpVal: e.detail
    })
  },
  sureChange(e) {
    let {
      tmpVal,
      prop,
      currentDate
    } = this.data
    console.log(tmpVal, prop)
    if (prop === 'birth') {
      tmpVal = formatTime(new Date(currentDate)).substr(0, 10).replace(/\//g, '-')
    }
    if (!tmpVal) {
      wx.showToast({
        title: '字段不能为空',
        icon: 'none'
      })
      return;
    }


    this.setData({
      [prop]: tmpVal,
      prop: '',
      type: '',
      columns: [],
      popupShow: false,
      tmpVal: '',

    })

  },
  pickerChange(e) {
    console.log(e)
    const {
      index,
      value
    } = e.detail
    const {
      prop
    } = this.data

    if (prop === 'gender') {
      this.setData({
        tmpVal: Number(index) + 1
      })
    }
  },
  onInput(e) {
    console.log(e)

    this.setData({
      currentDate: e.detail,
    });
  },

  upload() {
    const {
      avatar
    } = this.data

    uploadSingleImg().then(res => {
        console.log(res)
        this.setData({
          avatar: res.imageURL
        })
        wx.showToast({
          title: '上传成功',
          icon: 'none'
        })
      })
      .catch(err => {
        console.log(err)
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        })
      })
  },
  toAddress() {
    wx.navigateTo({
      url: '/pages/address/address',
    })
  }
})